#!/bin/bash

APP_NAME=${1}
APP_REPO=${2}
APP_BRANCH=${3}

[ "${APP_BRANCH}" ] && BRANCH="-b ${APP_BRANCH}"

echo -e "frappe\n${APP_NAME}" > /home/frappe/frappe-bench/sites/apps.txt
cd /home/frappe/frappe-bench/apps
git clone --depth 1 ${APP_REPO} ${APP_NAME} ${BRANCH}
cd /home/frappe/frappe-bench/apps/${APP_NAME} && yarn --pure-lockfile

cd /home/frappe/frappe-bench/apps/frappe


yarn config delete proxy

yarn config set registry https://mirrors.huaweicloud.com/repository/npm/
# yarn config set registry https://registry.npm.taobao.org
yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g 
yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g

yarn config list

yarn production --app ${APP_NAME}

mkdir -p /home/frappe/frappe-bench/sites/assets/${APP_NAME}
cp -R /home/frappe/frappe-bench/apps/${APP_NAME}/${APP_NAME}/public/* /home/frappe/frappe-bench/sites/assets/${APP_NAME} 2>/dev/null || :
cp -R /home/frappe/frappe-bench/apps/${APP_NAME}/node_modules /home/frappe/frappe-bench/sites/assets/${APP_NAME}/ 2>/dev/null || :

echo "rsync -a --delete /var/www/html/assets/${APP_NAME} /assets" >> /rsync
chmod +x /rsync
